package com.xique.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.bean.domain.ParkOnV1;
import com.xique.park.bean.request.comm.ParkOnListRequest;
import com.xique.park.controller.client.web.bean.search.ParkOnSearch;
import com.xique.park.controller.client.web.bean.vo.ClientHandOutParkInInfoVo;
import com.xique.park.mapper.ParkOnMapper;
import com.xique.park.mapper.ParkOnMapperV1;
import com.xique.park.service.service.IParkOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 在场车辆Service业务层处理
 *
 * @author xique
 * @date 2021-12-09
 */
@Service
public class ParkOnServiceImpl implements IParkOnService {

    @Autowired
    private ParkOnMapper parkOnMapper;

    @Autowired
    private ParkOnMapperV1 parkOnMapperV1;

    /**
     * 查询在场车辆
     *
     * @param id 在场车辆ID
     * @return 在场车辆
     */
    @Override
    public ParkOn selectParkOnById(Long id) {
        return parkOnMapper.selectById(id);
    }

    /**
     * 查询在场车辆列表
     *
     * @param parkOn 在场车辆
     * @return 在场车辆
     */
    @Override
    public List<ParkOn> selectParkOnList(ParkOn parkOn) {
        return parkOnMapper.selectParkOnList(parkOn);
    }

    @Override
    public List<ParkOn> selectParkOnAllList(ParkOnListRequest request) {
        return parkOnMapper.selectParkOnAllList(request);
    }

    @Override
    public IPage<ParkOn> selectParkOnPage(ParkOnListRequest search) {
        Page<ParkOn> page = new Page<>();
        page.setCurrent(search.getPageNum());
        page.setSize(search.getPageSize());
        return parkOnMapper.selectParkOnPage(page, search);
    }

    /**
     * 新增在场车辆
     *
     * @param parkOn 在场车辆
     * @return 结果
     */
    @Override
    public int insertParkOn(ParkOn parkOn) {
        parkOn.setCreateTime(DateUtils.getNowDate());
        return parkOnMapper.insert(parkOn);
    }

    /**
     * 修改在场车辆
     *
     * @param parkOn 在场车辆
     * @return 结果
     */
    @Override
    public int updateParkOn(ParkOn parkOn) {
        parkOn.setUpdateTime(DateUtils.getNowDate());
        return parkOnMapper.updateById(parkOn);
    }

    /**
     * 批量删除在场车辆
     *
     * @param ids 需要删除的在场车辆ID
     * @return 结果
     */
    @Override
    public int deleteParkOnByIds(List<Long> ids) {
        return parkOnMapper.deleteBatchIds(ids);
    }

    /**
     * 删除在场车辆信息
     *
     * @param id 在场车辆ID
     * @return 结果
     */
    @Override
    public int deleteParkOnById(Long id) {
        return parkOnMapper.deleteById(id);
    }

    @Override
    public List<ParkOn> selectBatchIds(List<Long> ids) {
        return parkOnMapper.selectBatchIds(ids);
    }

    @Override
    public Long selectParkOnCount(ParkOn parkOn) {
        return parkOnMapper.selectParkOnCount(parkOn);
    }

    @Override
    public List<ParkOn> selectParkOnByPlate(Long parkId, String plate) {
        return parkOnMapper.selectParkOnByPlate(parkId, plate);
    }

    @Override
    public List<ClientHandOutParkInInfoVo> getHandOutParkInInfoVoList(ParkOn parkOn) {
        return parkOnMapper.getHandOutParkInInfoVoList(parkOn);
    }

    @Override
    public ParkOnV1 selectParkOn(ParkOnSearch search) {
        QueryWrapper<ParkOnV1> wrapper = new QueryWrapper<>();
        if (null != search.getId()) {
            wrapper.eq("id", search.getId());
        }
        if (StringUtils.isNotEmpty(search.getParkingSerial())) {
            wrapper.eq("parking_serial", search.getParkingSerial());
        }
        wrapper.orderByDesc("UNIX_TIMESTAMP(update_time)")
                .last("limit 1");

        return parkOnMapperV1.selectOne(wrapper);
    }

    @Override
    public ParkOn selectParkOnAndParkInById(Long id) {
        return parkOnMapper.selectParkOnAndParkInById(id);
    }

    @Override
    public List<ParkOn> selectParkOnListBeforeDay(Long parkId, String beforeDateStr) {
        return parkOnMapper.selectParkOnListBeforeDay(parkId, beforeDateStr);
    }

    @Override
    public List<ParkOn> selectParkOnByPlateList(Long parkId, List<String> plateList) {
        return parkOnMapper.selectParkOnByPlateList(parkId, plateList);
    }
}
